package com.ruoyi.dynamic.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.enums.CacheKey;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.redis.cache.CacheTemplate;
import com.ruoyi.dynamic.mapper.RoomSetMapper;
import com.ruoyi.dynamic.model.constant.DynamicConstant;
import com.ruoyi.dynamic.model.pojo.entity.Gift;
import com.ruoyi.dynamic.model.pojo.entity.RoomSet;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomSetUpdateRequest;
import com.ruoyi.dynamic.model.pojo.result.app.AppRoomSetResult;
import com.ruoyi.dynamic.service.IGiftService;
import com.ruoyi.dynamic.service.IRoomQuizRecordService;
import com.ruoyi.dynamic.service.IRoomService;
import com.ruoyi.dynamic.service.IRoomSetService;
import com.ruoyi.system.api.model.user.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.concurrent.TimeUnit;

@Service
public class RoomSetServiceImpl extends ServiceImpl<RoomSetMapper, RoomSet> implements IRoomSetService {

    @Autowired
    private IGiftService giftService;
    @Autowired
    @Lazy
    private IRoomQuizRecordService iRoomQuizRecordService;


    @Override
    public AppRoomSetResult getUserRoomSet(Long userId) {
        return CacheTemplate.getCache(CacheKey.ROOM_SET.getKey(String.valueOf(userId)), AppRoomSetResult.class, () -> {
            RoomSet one = lambdaQuery().eq(RoomSet::getUserId, userId)
                    .one();
            if (one == null) {
                one = new RoomSet();
                one.setUserId(userId);
                save(one);
                one = getById(one.getUserId());
            }
            return getRoomSet(one);
        });
    }

    @Override
    public AppRoomSetResult updateRoomSet(Long userId, AppRoomSetUpdateRequest request) {
        lambdaUpdate()
                .eq(RoomSet::getUserId, userId)
                .set(!CommonUtils.isNumber(request.getIsGive()), RoomSet::getIsGive, request.getIsGive())
                .set(!CommonUtils.isNumber(request.getIsWish()), RoomSet::getIsWish, request.getIsWish())
                .set(!CommonUtils.isNumber(request.getIsProgram()), RoomSet::getIsProgram, request.getIsProgram())
                .set(!CommonUtils.isNumber(request.getIsVote()), RoomSet::getIsVote, request.getIsVote())
                .set(!CommonUtils.isNumber(request.getIsLuckyBag()), RoomSet::getIsLuckyBag, request.getIsLuckyBag())
                .set(!CommonUtils.isNumber(request.getIsInteract()), RoomSet::getIsInteract, request.getIsInteract())
                .set(!CommonUtils.isNumber(request.getPkTime()), RoomSet::getPkTime, request.getPkTime())
                .set(!CommonUtils.isNumber(request.getIsGiveAim()), RoomSet::getIsGiveAim, request.getIsGiveAim())
                .set(!CommonUtils.isNumber(request.getIsLigature()), RoomSet::getIsLigature, request.getIsLigature())
                .set(!CommonUtils.isNumber(request.getIsInterLigature()), RoomSet::getIsInterLigature, request.getIsInterLigature())
                .set(!CommonUtils.isNumber(request.getIsPopuLigature()), RoomSet::getIsPopuLigature, request.getIsPopuLigature())
                .set(!CommonUtils.isNumber(request.getIsRecSearchLigature()), RoomSet::getIsRecSearchLigature, request.getIsRecSearchLigature())
                .set(!CommonUtils.isNumber(request.getIsRandomCover()), RoomSet::getIsRandomCover, request.getIsRandomCover())
                .set(!CommonUtils.isNumber(request.getIsLiveLocation()), RoomSet::getIsLiveLocation, request.getIsLiveLocation())
                .set(StringUtils.hasText(request.getRoomTitle()), RoomSet::getRoomTitle, request.getRoomTitle())
                .set(StringUtils.hasText(request.getRoomContent()), RoomSet::getRoomContent, request.getRoomContent())
                .set(StringUtils.hasText(request.getRoomCoverImg()), RoomSet::getRoomCoverImg, request.getRoomCoverImg())
                .set(!CommonUtils.isNumber(request.getIsQuiz()), RoomSet::getIsQuiz, request.getIsQuiz())
                .set(!CommonUtils.isNumber(request.getIsLienMai()), RoomSet::getIsLienMai, request.getIsLienMai())
                .set(StringUtils.hasText(request.getQuizContent()), RoomSet::getQuizContent, request.getQuizContent())
                .set(!CommonUtils.isNumber(request.getIsComment()), RoomSet::getIsComment, request.getIsComment())
                .set(!CommonUtils.isNumber(request.getCommentType()), RoomSet::getCommentType, request.getCommentType())
                .set(!CommonUtils.isNumber(request.getIsPayLigature()), RoomSet::getIsPayLigature, request.getIsPayLigature())
                .set(!CommonUtils.isBigDecimal(request.getLigatureMoney()), RoomSet::getLigatureMoney, request.getLigatureMoney())
                .set(!CommonUtils.isNumber(request.getLigatureTime()), RoomSet::getLigatureTime, request.getLigatureTime())
                .set(!CommonUtils.isNumber(request.getIsRelationCheck()), RoomSet::getIsRelationCheck, request.getIsRelationCheck())
                .set(!CommonUtils.isNumber(request.getIsElseCheck()), RoomSet::getIsElseCheck, request.getIsElseCheck())
                .set(!CommonUtils.isNumber(request.getAuctionMinNum()), RoomSet::getAuctionMinNum, request.getAuctionMinNum())
                .set(!CommonUtils.isNumber(request.getQuizGiftId()), RoomSet::getQuizGiftId, request.getQuizGiftId())
                .set(StringUtils.hasText(request.getCommentContent()), RoomSet::getCommentContent, request.getCommentContent())
                .set(StringUtils.hasText(request.getQuizGiftImg()), RoomSet::getQuizGiftImg, request.getQuizGiftImg())
                .set(StringUtils.hasText(request.getProgramContent()), RoomSet::getProgramContent, request.getProgramContent())
                .set(!CommonUtils.isNumber(request.getIsSingMusic()), RoomSet::getIsSingMusic, request.getIsSingMusic())
                .set(!CommonUtils.isNumber(request.getIsRoomQuiz()), RoomSet::getIsRoomQuiz, request.getIsRoomQuiz())
                .update();
        RoomSet one = getOneByUser(userId);
        if (!CommonUtils.isNull(request.getIsQuiz())) {
            iRoomQuizRecordService.openOrStop(one.getIsQuiz(), request.getRooomId(), one.getQuizContent(),one.getQuizGiftId(),one.getQuizGiftImg());
        }
        return getRoomSet(one);
    }

    @Override
    public AppRoomSetResult setGift(Long userId, Long giftId) {

        Gift verify = giftService.verify(giftId);
        RoomSet oneByUser = getOneByUser(userId);
        oneByUser.setQuizGiftImg(verify.getGiftImg());
        oneByUser.setQuizGiftId(giftId);
        updateById(oneByUser);
        return getRoomSet(oneByUser);
    }

    @Override
    public RoomSet getOneByUser(Long userId) {
        RoomSet one = lambdaQuery().eq(RoomSet::getUserId, userId).one();
        if (one == null) {
            one = new RoomSet();
            one.setUserId(userId);
            save(one);
            return one;
        }
        return one;
    }

    @Override
    public AppRoomSetResult getRoomSet(RoomSet roomSet) {
        AppRoomSetResult appRoomSetResult = new AppRoomSetResult();
        BeanUtils.copyProperties(roomSet, appRoomSetResult);
        CacheTemplate.setCache(CacheKey.ROOM_SET.getKey(String.valueOf(roomSet.getUserId())), appRoomSetResult, 30L, TimeUnit.DAYS);
        return appRoomSetResult;
    }
}
